package com.iwhalecloud.bss.kite.cucc.web.service.accept.service;

import com.iwhalecloud.bss.kite.common.cache.AbstractCache;
import com.iwhalecloud.bss.kite.common.cache.enums.CacheMode;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.common.util.TransactionUtils;
import com.iwhalecloud.bss.kite.cucc.web.service.scene.PageNavigationService;
import com.iwhalecloud.bss.kite.web.service.accept.vo.PageNavigationVO;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 *
 * <Description> 页面导航缓存 <br>
 *
 * @author huang.caibiao<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2019年6月4日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.dataservice.cache <br>
 */
@Lazy
@Component
public class CuccKitePageNavigationCache extends AbstractCache {

    @Override
    public CacheMode getCacheMode() {
        return CacheMode.LOCAL;
    }

    public static CuccKitePageNavigationCache getInst() {
        return SpringUtil.getBean(CuccKitePageNavigationCache.class);
    }

    public static PageNavigationVO get(String serviceOfferId, String tacheCode, String offerType, String objId) {
        if (KiteStringUtils.isAnyBlank(serviceOfferId, tacheCode, offerType)) {
            return null;
        }
        // 互联网不同省模板不同，缓存key新增省编码
        String provinceNbr = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        String key = KiteStringUtils.join(serviceOfferId, "_", tacheCode, "_", offerType, "_", provinceNbr);
        if (!KiteStringUtils.isEmpty(objId)) {
            key = KiteStringUtils.join(key, "_", objId);
        }
        return getInst().defaultGetObject(key);
    }

    @SuppressWarnings("unchecked")
    @Override
    public PageNavigationVO getObject(String key) {
        return TransactionUtils.execute(() -> {
            String[] keys = key.split("_");
            String objId = null;
            if (keys.length == 5) {
                objId = keys[4];
            }
            return SpringUtil.getBean(PageNavigationService.class).getPageNavigation(keys[0], keys[1], keys[2], keys[3], objId);
        });
    }
}
